package com.wpre.booksystem.config;

import com.wpre.booksystem.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-11-24
 * Time: 14:38
 */

@Slf4j
@ControllerAdvice
@ResponseBody//返回数据,Result.fail()是数据
public class ExceptionAdvice {
    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object handler(Exception e) {
        log.error("发生异常,e", e);
        return Result.fail();
    }

    @ExceptionHandler
    public Object handler(NullPointerException e){
        log.error("发生异常, e: ", e);
        return Result.fail("发生NullPointerException 异常");
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler
    public Object handler(NoResourceFoundException e){
        log.error("发生异常, e: {}, path:{}", e.getDetailMessageCode(), e.getResourcePath());
        return Result.fail("发生NoResourceFoundException 异常");
    }
}
